package com.glsc.ngateway.common.base.repo.mysql.gateway.platform;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.MysqlDataDictionary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface MysqlDataDictionaryRepository extends JpaRepository<MysqlDataDictionary, Long> {

    public List<MysqlDataDictionary> findByFieldName(String fieldName);

    @Transactional
    @Modifying
    @Query(value = "delete from MysqlDataDictionary d where d.dictSpace = :dictSpace and d.fieldName = :fieldName")
    int deleteById(@Param("dictSpace") String dictSpace, @Param("fieldName") String fieldName);

    @Transactional
    @Modifying
    @Query(value = "delete from MysqlDataDictionary d where d.dicId NOT IN (:dicIds) and d.fieldName = :fieldName and d.dictSpace = :dictSpace")
    int deleteById(@Param("dicIds") List<Long> dicIds, @Param("fieldName") String fieldName, @Param("dictSpace") String dictSpace);

    @Query(value = "select max(fOrder) from MysqlDataDictionary d where d.fieldName = :fieldName and d.dictSpace = :dictSpace")
    int findMaxOrder(@Param("fieldName") String fieldName, @Param("dictSpace") String dictSpace);
}
